package gui.edit.components;

import gui.edit.enums.E_EditParamsSliders;
import gui.edit.listeners.ListenerFillButton;
import gui.edit.listeners.ListenerParamSliderChange;
import gui.edit.listeners.ListenerHelpButton;
import gui.utils.IntegerField;
import gui.visu.listeners.ListenerIntegerField;
import gui.visu.listeners.ListenerSlider;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import tuio.components.ButtonTouch;
import tuio.components.SliderTouch;

import controls.edit.ParamControl;

import data.Constants;
import data.visu.utils.ImageLoader;
import data.visu.utils.StringFormater;

/**
 * @author Charles Perin
 * 29 mars 2011
 * LIMSI - INRIA Rennes
 * charles.perin@free.fr
 *
 */
public abstract class A_ParamsSliders_panel extends JPanel implements I_ParamsSliders_panel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7421198296553228563L;
	
	protected JLabel name;
	
	protected JTextField field;

	protected SliderTouch slider;
	
	protected ButtonTouch buttonHelp;
	
	protected ButtonTouch buttonFill;
	
	protected E_EditParamsSliders editParam;
	
	protected ParamControl controler;
	
	public A_ParamsSliders_panel(SliderTouch slider, E_EditParamsSliders editParam, ParamControl controler){
		this.editParam=editParam;
		this.slider=slider;
		this.controler=controler;
		initComponents(editParam);
		
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		c.insets = new Insets(0, 0, 1, 0);
		c.gridx=0;
		c.gridy=0;
		c.weighty=0;
		c.fill=GridBagConstraints.BOTH;
		add(name, c);
		
		c.fill=GridBagConstraints.NONE;
		c.gridy+=1;
		add(buttonFill,c);
		c.gridx=1;
		add(buttonHelp,c);
		
		c.fill=GridBagConstraints.BOTH;
		c.gridx=0;
		c.weightx=0;
		c.gridy+=1;
		c.weighty=1;
		add(this.slider, c);

		c.gridy+=1;
		c.weighty=0;
		add(field, c);
		
		customizeSlider();
	}
	
	private void initComponents(E_EditParamsSliders editParam){
		String nameString = editParam.getName();
		
		nameString = StringFormater.formatString(nameString, 10);
		name = new JLabel(nameString);
		
		if(editParam.isIntegerValue()){
			field = new IntegerField();
			slider.addChangeListener(new ListenerSlider(slider, (IntegerField) field));
			field.addKeyListener(new ListenerIntegerField((IntegerField) field, slider));
		}
		else field = new JTextField();
		
		name.setPreferredSize(Constants.CAMERA_PANEL_DIMENSION_SLIDERS_NAMES);
		name.setFont(Constants.CAMERA_PANEL_FONT_SLIDERS_LABELS);
		
		field.setHorizontalAlignment(JTextField.CENTER);
		field.setBorder(BorderFactory.createLineBorder(Color.BLACK));
		if(editParam.isIntegerValue())field.setText(Double.toString(editParam.getStartValue()));
		field.setPreferredSize(Constants.CAMERA_PANEL_DIMENSION_SLIDERS_VALUES);
		field.setFont(Constants.CAMERA_PANEL_FONT_SLIDERS_VALUES);
		field.setEditable(editParam.isIntegerValue());
		
		slider.setValue(editParam.getStartValue());
		slider.setOrientation(SliderTouch.VERTICAL);
		slider.setPreferredSize(Constants.CAMERA_PANEL_DIMENSION_SLIDERS_V);
		slider.setVisible(true);
		slider.setMinimum(editParam.getMinValue());
		slider.setMaximum(editParam.getMaxValue());
		slider.setMajorTickSpacing(editParam.getMajorTickSpacing());
		slider.setPaintTrack(true);
		slider.setSnapToTicks(editParam.isSnapToTicks());
		slider.setToolTipText(editParam.getSliderToolTip());
		slider.addChangeListener(new ListenerParamSliderChange(this));
		slider.setPaintLabels(true);
		
		ImageIcon icon = ImageLoader.createImageIcon("help.PNG");
		buttonHelp = new ButtonTouch(icon);
		buttonHelp.setMargin(new Insets(0,0,0,0));
		buttonHelp.setPreferredSize(new Dimension(12,12));
		buttonHelp.addActionListener(new ListenerHelpButton(controler.getParamsControl().getDisplayDataList().getDataControl().getIhm(),editParam.getName(), editParam.getInfos(), null));
		
		buttonFill = new ButtonTouch("Fill");
		buttonFill.setMargin(new Insets(0,0,0,0));
		buttonFill.setPreferredSize(new Dimension(25,12));
		buttonFill.addActionListener(new ListenerFillButton(controler, editParam));
	}
	
	
	public JTextField getField() {
		return field;
	}

	public ParamControl getControler() {
		return controler;
	}
	

}
